<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="退款编号" prop="refundNo">
        <el-input
          v-model="queryParams.refundNo"
          placeholder="请输入退款编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="退款金额" prop="refundAmount">
        <el-input
          v-model="queryParams.refundAmount"
          placeholder="请输入退款金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="申请人" prop="applicantNickName">
        <el-input
          v-model="queryParams.applicantNickName"
          placeholder="请输入申请人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeApplicationTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="退款时间" prop="refundTime">
        <el-date-picker clearable
          v-model="queryParams.refundTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择退款时间">
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="订单表id" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单表id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="退款状态" prop="refundStatus">
        <el-select v-model="queryParams.refundStatus" placeholder="请选择退款状态" clearable>
          <el-option
            v-for="dict in refund_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="失败状态码" prop="failureStatusCode">
        <el-input
          v-model="queryParams.failureStatusCode"
          placeholder="请输入失败状态码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['refund:refund:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['refund:refund:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['refund:refund:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['refund:refund:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->
    <!-- 循环渲染订单状态按钮 -->
    <!-- 交易状态按钮 -->
   <div class="status-buttons">
    <!-- 全部按钮（值为''，对应默认状态） -->
    <div 
      class="status-btn" 
      :class="{ active: selectedStatus === '' }"
      @click="handleClick('')"
    >
      全部
    </div>
    <!-- 循环渲染订单状态按钮 -->
    <div 
      class="status-btn"
      v-for="dict in refund_status" 
      :key="dict.value"
      :class="{ active: selectedStatus === dict.value }"
      @click="handleClick(dict.value)"
    >
      {{ dict.label }}
    </div>
  </div>

    <el-table v-loading="loading" :data="refundList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center"><template #default="scope">
        {{ scope.$index + 1 }}
      </template></el-table-column>
      <el-table-column label="退款编号" align="center" prop="refundNo" width="150" />
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="退款金额" align="center" prop="refundAmount" />
      <el-table-column label="申请人" align="center" prop="sysUser.nickName" />
      <el-table-column label="申请时间" align="center" prop="applicationTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.applicationTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退款时间" align="center" prop="refundTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.refundTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderManagement">
        <template #default="scope">
          <dict-tag :options="order_status" :value="scope.row.orderManagement?.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="退款状态" align="center" prop="refundStatus">
        <template #default="scope">
          <dict-tag :options="refund_status" :value="scope.row.refundStatus"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="退款原因" align="center" prop="refundReason" />
      <el-table-column label="退款渠道" align="center" prop="refundChannel" />
      <el-table-column label="退款方式" align="center" prop="refundMethod" />
      <el-table-column label="失败状态码" align="center" prop="failureStatusCode" />
      <el-table-column label="失败原因" align="center" prop="failureReason" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['refund:refund:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['refund:refund:remove']">删除</el-button> -->
          <el-button link type="primary" @click="handleView(scope.row)" v-hasPermi="['refund:refund:view']">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退款记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="refundRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="退款编号" prop="refundNo">
          <el-input v-model="form.refundNo" placeholder="请输入退款编号" />
        </el-form-item>
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="退款金额" prop="refundAmount">
          <el-input v-model="form.refundAmount" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="申请人" prop="applicantName">
          <el-input v-model="form.applicantName" placeholder="请输入申请人" />
        </el-form-item>
        <el-form-item label="申请时间" prop="applicationTime">
          <el-date-picker clearable
            v-model="form.applicationTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退款时间" prop="refundTime">
          <el-date-picker clearable
            v-model="form.refundTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择退款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单表id" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单表id" />
        </el-form-item>
        <el-form-item label="退款状态" prop="refundStatus">
          <el-radio-group v-model="form.refundStatus">
            <el-radio
              v-for="dict in refund_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="退款原因" prop="refundReason">
          <el-input v-model="form.refundReason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="失败状态码" prop="failureStatusCode">
          <el-input v-model="form.failureStatusCode" placeholder="请输入失败状态码" />
        </el-form-item>
        <el-form-item label="失败原因" prop="failureReason">
          <el-input v-model="form.failureReason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
  <!-- 查看详情对话框 -->
   <el-dialog
    v-model="centerDialogVisible"
    width="500"
    align-center
  >
    <template #header>
      <div  class="dialog-title">
        <span>退款记录详情</span>
      </div>
      
    </template>
    <div class="lookDetail">
      <div>
        <div>退款编号</div>
        <div>{{form.refundNo}}</div>
      </div>
      <div>
        <div>订单编号</div>
        <div>{{form.orderNo}}</div>
      </div>
      <div>
        <div>订单状态</div>
        <div><dict-tag :options="order_status" :value="form.orderManagement?.orderStatus"/></div>
      </div>
      <div>
        <div>退款状态</div>
        <div><dict-tag :options="refund_status" :value="form.refundStatus"/></div>
      </div>
      <div>
        <div>申请人</div>
        <div>
          <div>{{form.sysUser?.nickName}}<span class="background-slogin">后台用户</span></div>
        </div>
      </div>
      <div>
        <div>申请时间</div>
        <div>{{form.applicationTime}}</div>
      </div>
      <div>
        <div>退款原因</div>
        <div>{{form.refundReason}}</div>
      </div>
      <div>
        <div>退款渠道</div>
        <div>{{form.refundChannel}}</div>
      </div>
      <div>
        <div>退款方式</div>
        <div>{{form.refundMethod}}</div>
      </div>
      <div v-if="form.refundStatus == 0">
        <div>退款时间</div>
        <div>{{parseTime(form.refundTime)}}</div>
      </div>
      <div  v-if="form.refundStatus == 0">
        <div>退款金额</div>
        <div>{{form.refundAmount}} 元
        </div>
      </div>
      <div v-if="form.refundStatus == 2">
        <div>失败状态码</div>
        <div>{{form.failureStatusCode}}</div>
      </div>
      <div v-if="form.refundStatus == 2">
        <div>失败原因</div>
        <div>{{form.failureReason}}</div>
      </div>
    </div>
  </el-dialog>
</template>

<script setup name="Refund">
import { listRefund, getRefund, delRefund, addRefund, updateRefund } from "@/api/orderManagement/refund"
const { proxy } = getCurrentInstance()
const { order_status,refund_status } = proxy.useDict('order_status','refund_status')
// 查看详情对话框
const centerDialogVisible = ref(false)

const refundList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const daterangeApplicationTime = ref([])
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    refundNo: null,
    orderNo: null,
    refundAmount: null,
    applicant: null,
    applicantNickName: null,
    applicationTime: null,
    refundTime: null,
    orderId: null,
    refundStatus: null,
    refundReason: null,
    refundChannel: null,
    refundMethod: null,
    failureStatusCode: null,
    failureReason: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询退款记录列表 */
function getList() {
  loading.value = true
  queryParams.value.params = {}
  if (null != daterangeApplicationTime && '' != daterangeApplicationTime) {
    queryParams.value.params["beginApplicationTime"] = daterangeApplicationTime.value[0]
    queryParams.value.params["endApplicationTime"] = daterangeApplicationTime.value[1]
  }
  listRefund(queryParams.value).then(response => {
    refundList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    refundNo: null,
    orderNo: null,
    refundAmount: null,
    applicant: null,
    applicationTime: null,
    refundTime: null,
    orderId: null,
    refundStatus: null,
    refundReason: null,
    refundChannel: null,
    refundMethod: null,
    failureStatusCode: null,
    failureReason: null,
    extraField1: null,
    extraField2: null
  }
  proxy.resetForm("refundRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeApplicationTime.value = []
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加退款记录"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getRefund(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改退款记录"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["refundRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateRefund(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addRefund(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除退款记录编号为"' + _ids + '"的数据项？').then(function() {
    return delRefund(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('refund/refund/export', {
    ...queryParams.value
  }, `refund_${new Date().getTime()}.xlsx`)
}

getList()
/** 查看按钮操作 */
function handleView(row) {
  reset()
  const _id = row.id || ids.value
  getRefund(_id).then(response => {
    form.value = response.data
    centerDialogVisible.value = true
    title.value = "查看退款记录"
  })
}

// 记录当前选中的状态（默认选中“全部”，对应值''）
const selectedStatus = ref('');

// 点击按钮触发的函数（接收传递的值）
const handleClick = (value) => {
  // 更新选中状态
  selectedStatus.value = value;
  // 执行后续逻辑（如查询、筛选等）
  queryParams.value.refundStatus = value
  console.log('当前选中的值：', value);
  // 可在这里调用接口或其他业务函数
  getList()
};
</script>
<style scoped>
.dialog-title{
  font-size: 18px;
  font-weight: bold;
}
/* 查看弹窗样式 */

.lookDetail>div{
  display: flex;
}
.lookDetail>div>div:nth-child(1){
  width: 100px;
  height: 40px;
  line-height: 40px;
  text-align: right;
  margin-right: 30px;
  color: #909399;
}
.lookDetail>div>div:nth-child(2){
  width: 300px;
  height: 40px;
  line-height: 40px;
  margin-left: 50px;
  color: #909399;
  background-color: white;
}
.background-slogin{
  font-size: 12px;
  color: #F08F4D;
  background-color: #FEF3E6;
  padding: 5px 4px;
  border-radius: 4px;
  margin-left: 20px;
}

  .status-buttons {
  display: flex;  /* 横向排列 */
  gap: 12px;      /* 按钮之间的间距 */
  padding: 5px 5px;
  background-color: #F2F2F2;
  margin-top: 5px;
  margin-bottom: 5px;
  width: 385px;
}

.status-btn {
  /* 基础样式 */
  padding: 6px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  color: black;  /* 未选中文字颜色 */
  transition: all 0.2s;  /* 过渡动画 */
}

/* 选中样式（通过active类控制） */
.status-btn.active {
  background: white;  /* 选中背景色（自定义） */
  color: black;          /* 选中文字颜色 */
  /* 可选：添加阴影增强选中效果 */
  box-shadow: 0 2px 4px rgba(64, 158, 255, 0.2);
}

/* 鼠标悬停样式 */
.status-btn:not(.active):hover {
  background: #e9e9e9;  /* 未选中时的悬停背景 */
}

</style>
